/*****************************
 *  -----------------------  *
 * DRobot (Take the Control) *
 *  -----------------------  *
 *****************************/
package com.drobot.clipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Clipboard {
	
	private java.awt.datatransfer.Clipboard cb;
	
	public Clipboard() {
		cb = Toolkit.getDefaultToolkit().getSystemClipboard();
	}

	/**
	 * 
	 * Set the text on the windows clipboard
	 * 
	 * @param text
	 */
	public void setClipboard(String text){
		StringSelection paste = new StringSelection(text);
		cb.setContents(paste, paste);
	}
	
	
	/**
	 * 
	 * Get the text from the windows clipboard
	 * 
	 * @return text (success), null (fail)
	 */
	public String getClipboard(){
		Transferable dato = cb.getContents(0);
		try {
			DataFlavor dataFlavorStringJava;
			dataFlavorStringJava = new DataFlavor("application/x-java-serialized-object; class=java.lang.String");
			if (dato.isDataFlavorSupported(dataFlavorStringJava)) {
			   String texto = (String) dato.getTransferData(dataFlavorStringJava);
			   return texto;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedFlavorException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
